package labb3;

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Shows a JDialog giving the user the possibility to change the specified Media
 * object.
 *
 * @author Mikko Puustinen
 * @version 0.3, 2007-okt-18
 */
public class AddEditMedia extends JDialog implements Labels, ActionListener {
    private JComponent title;
    private JTextField prodYear;
    private JTextField relYear;
    private JTextField purDate;
    private JComboBox rating;
    private JButton addButton;
    private JButton cancelButton;
    private MediaController controller;
    private String[] ratings = new String[11];
    private Point location;
    private Media media;

    /**
     * Creates the JDialog and adds all the information about the specified
     * Media object. All input fields and buttons are able to listen for the
     * ENTER-key to be pressed.
     *
     * @param controller reference to the MediaController who handles the data
     *                   to the MediaCollection.
     * @param media      Media object to edit.
     * @param location   location of the parent window.
     *
     * @see labb3.MediaController
     * @see javax.swing.JDialog
     */
    public AddEditMedia(Point location, MediaController controller, Media media) {
        this.location = location;
        this.controller = controller;
        this.media = media;
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    public AddEditMedia(Point location, MediaController controller) {
        this(location, controller, null);
    }

    public void inputDialog() {
        EditKeyListener ekl = new EditKeyListener();
        int x = (int)location.getX() + 100;
        int y = (int)location.getY() + 100;

        for (int i = 0; i < ratings.length; i++)
            ratings[i] = String.valueOf(i);

        JPanel panel = new JPanel(new GridLayout(6, 2));
        add(panel);

        if (media == null) {
            title = new JTextField(15);
            prodYear = new JTextField(15);
            relYear = new JTextField(15);
            purDate = new JTextField(15);
            rating = new JComboBox(ratings);
        } else {
            title = new JLabel(media.getTitle());
            prodYear = new JTextField(media.getProductionDate());
            relYear = new JTextField(media.getReleaseDate());
            purDate = new JTextField(media.getPurchasedDate());
            rating = new JComboBox(ratings);
            rating.setSelectedIndex(Integer.parseInt(media.getRating()));
        }

        JTextField[] fields = {prodYear, relYear, purDate};
        JLabel titleLabel = new JLabel(TITLE);
        JLabel prodLabel = new JLabel(PROD_DATE);
        JLabel relLabel = new JLabel(REL_DATE);
        JLabel purLabel = new JLabel(PUR_DATE);
        JLabel ratingLabel = new JLabel(RATING);

        addButton = new JButton("Submit");
        addButton.addActionListener(this);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);

        for (JTextField jtf : fields)
            jtf.addKeyListener(ekl);
        title.addKeyListener(ekl);
        addButton.addKeyListener(ekl);
        cancelButton.addKeyListener(ekl);

        panel.add(titleLabel);
        panel.add(title);
        panel.add(prodLabel);
        panel.add(prodYear);
        panel.add(relLabel);
        panel.add(relYear);
        panel.add(purLabel);
        panel.add(purDate);
        panel.add(ratingLabel);
        panel.add(rating);
        panel.add(addButton);
        panel.add(cancelButton);
        setLocation(x, y);
        setResizable(false);
        if (media == null)
            setTitle("Add media");
        else
            setTitle("Edit media");
        pack();
        setVisible(true);
    }

    /**
     * Helper method to get the text from all the input fields and post it to
     * the MediaController. If media is null we are going to add a new element
     * to the MediaCollection. Before adding the collection will be checked for
     * Media with the same title.
     */
    private void submit() {
        String strTitle = "";
        if (media == null) {
            strTitle = ((JTextField)title).getText();
            if (controller.isTitleInCollection(strTitle)) {
                JOptionPane.showMessageDialog(this, "Title already exist");
                return;
            }
        } else
            strTitle = ((JLabel)title).getText();

        try {
            controller.handleMedia(strTitle, prodYear.getText(), relYear.getText(),
                purDate.getText(), (String)rating.getSelectedItem());
        } catch (IllegalArgumentException e1) {
            //TODO: error handling class
            JOptionPane.showMessageDialog(this, "Input error:\n" + e1.getMessage());
            return;
        }
        dispose();
    }

    /**
     * Invoked when an action occurs.
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cancelButton) {
            dispose();
        } else if (e.getSource() == addButton) {
            submit();
        }

    }

    private class EditKeyListener extends KeyAdapter {
        /**
         * Invoked when a key has been pressed. See the class description for
         * {@link KeyEvent} for a definition of a key pressed event.
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                if (e.getSource() == cancelButton)
                    dispose();
                else
                    submit();
            }
        }
    }
}
